package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private CategoryService categoryService;
    @PostMapping
    public R<String> save(@RequestBody DishDto dishDto){
        dishService.saveDishAndDishFlavor(dishDto);
        return R.success("保存菜品成功");
    }

    @GetMapping("/page")
    public R<Page> page(Integer page, Integer pageSize, String name) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        queryWrapper.orderByDesc(Dish::getUpdateTime);
        Page<Dish> dishPageInfo = new Page<>(page, pageSize);
        dishService.page(dishPageInfo,queryWrapper);
        //DishDto对象
        Page<DishDto> dishDtoPageInfo = new Page<>(page, pageSize);
        //copy属性
        BeanUtils.copyProperties(dishPageInfo,dishDtoPageInfo,"records");
        List<DishDto> dishDtoList = dishPageInfo.getRecords().stream().map(dish -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish, dishDto);
            //填充categoryName
            Long categoryId = dish.getCategoryId();
            Category category = categoryService.getById(categoryId);
            if (category != null) {
                dishDto.setCategoryName(category.getName());
            }
            return  dishDto;
        }).collect(Collectors.toList());
        dishDtoPageInfo.setRecords(dishDtoList);
        return R.success(dishDtoPageInfo);
    }
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getStatus,1);
        //select *from dish where id in ids;
        queryWrapper.in(Dish::getId,ids);
        int count = dishService.count(queryWrapper);
        if (count>0){
            throw new CustomException("菜品在售，不能删除");
        }
        dishService.deleteDishAndDishFlavors(ids);
        return R.success("删除成功");
    }
    @GetMapping("/{id}")
    public R<DishDto> getDishDtoById(@PathVariable  Long id){
        DishDto dishDto = dishService.getDishWithFlavors(id);
        return R.success(dishDto);
    }

    @PutMapping
    public R<DishDto> update(@RequestBody DishDto dishDto){
        DishDto dishDto1 = dishService.updateDishAndFlavors(dishDto);
        return R.success(dishDto1);
    }

    @PostMapping("/status/{statusNum}")
    public R<String> status (@PathVariable Integer statusNum, @RequestParam List<Long> ids){
        LambdaUpdateWrapper<Dish> dishLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        dishLambdaUpdateWrapper.in(Dish::getId,ids);
        dishLambdaUpdateWrapper.set(Dish::getStatus,statusNum);
        dishService.update(dishLambdaUpdateWrapper);
        return R.success(statusNum==0?"停售成功":"启售成功");
    }

    @GetMapping("/list")
    public R<List<DishDto>> list(Dish dish){
//        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Dish::getCategoryId,categoryId);
//        queryWrapper.eq(Dish::getStatus,1);
//        List<Dish> list = dishService.list(queryWrapper);
//
//        return R.success(list);
        List<DishDto> dishDtoList = dishService.getDishDtoByCategoryId(dish);
        return R.success(dishDtoList);
    }
}
